<?php


namespace App\Console\Crawle\Article;

use App\Console\Crawle\Grab;
use Illuminate\Support\Facades\Storage;

class Xenicalshop extends Grab
{

    protected function getArticleLinks(){
        $url = "https://www.xenicalshop.com/sitemap.xml";

        $sitemap = $this->client->get($url);

        $xml =simplexml_load_string($sitemap->getBody()->getContents()); //xml转object

        $xml= json_encode($xml);  //objecct转json

        $xml=json_decode($xml,true); //json转array
        $urls = [];
        if(isset($xml['url'])){

            foreach ($xml['url'] as $item){

                if(preg_match("/https:\/\/www\.xenicalshop\.com\/news\/\d*$/",$item['loc'])){
                    $urls[] = $item['loc'];
                }

            }
        }

        return $urls;
    }

    protected function getArticleContent($url){
        $response = $this->client->get($url);
        $content = $response->getBody()->getContents();

        preg_match("#<h1 (.*)>([^<]*)</h1>#",$content,$match);
        $title = $match[2];

        preg_match('/<iframe .*?src="(.*?)".*?><\/iframe>/is',$content,$match);
        $iframe_src = $match[1];





        $response = $this->client->get($iframe_src);
        $article = $response->getBody()->getContents();

        $doc = new \DOMDocument();
        libxml_use_internal_errors(true);
        $doc->loadHTML($article); // loads your html
        $xpath = new \DOMXPath($doc);

        $masterNode = $xpath->query('//div[@class="page-container"]/*');
        $htmlString = $doc->saveHTML($masterNode->item(0));


        preg_match_all('/<img.*?src="(.*?)".*?>/',$htmlString,$match);


        $image_data = [];
        if(isset($match[1])){
            $images = $match[1];

            $img_path = str_replace('index.html','',$iframe_src);

            foreach($images as $img){
                $origin = $img;
                $img = trim(trim($img,'.'),'/');
                $img_url = $img_path.$img;
                $arr = explode('.',$img);
                $ex = array_pop($arr);
                $path = 'crawle/'.uniqid().'.'.$ex;
                $data = $this->client->get($img_url)->getBody()->getContents();
                Storage::disk('local')->put($path,$data);

                $image_data[] = [
                    'origin'=>$origin,
                    'now'=>'/uploads/'.$path,
                ];
            }
        }

        foreach ($image_data as $img){
            $htmlString = str_replace($img['origin'],$img['now'],$htmlString);
        }

        return ['title'=>$title,'content'=>$htmlString];

    }




}
